package cn.tedu.ivos.audit.service.Impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.dto.AuditParam;
import cn.tedu.ivos.audit.pojo.dto.AuditQuery;
import cn.tedu.ivos.audit.pojo.entity.Audit;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.base.enums.AuditStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AuditServiceImpl implements AuditService {
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ApplicationMapper applicationMapper;

    @Override
    public int saveAudit(AuditParam auditParam) {
        log.debug("开始执行【保存审批单】业务层方法，参数：{}", auditParam);
        Audit audit = new Audit();
        BeanUtils.copyProperties(auditParam,audit);
        if(audit.getId()==null){
            audit.setCreateTime(new Date());
            return auditMapper.insertAudit(audit);
        }else {
            return 0;
        }

    }

    @Override
    public List<AuditVO> selectAuditByApplicationId(Long id) {
        log.debug("开始执行【根据申请单id查询审批单】业务层方法，参数：{}", id);

        return auditMapper.selectAuditByApplicationId(id);
    }

    @Override
    public List<AuditVO> selectAudit(AuditQuery auditQuery) {
        List<AuditVO> auditVOList = auditMapper.selectAudit(auditQuery);
        for(int i=0;i<auditVOList.size();i++){
            AuditVO auditVO = auditVOList.get(i);
            assignAuditName(auditVO);
        }
        return auditVOList;
    }

    @Override
    public int updateAudit(AuditParam auditParam) {
        log.debug("开始执行【修改审批单】业务层方法，参数：{}", auditParam);
        Audit audit = new Audit();
        BeanUtils.copyProperties(auditParam,audit);
        audit.setUpdateTime(new Date());
        int row=auditMapper.update(audit);
        //更新当前审批单后,准备更新当前审批单对应的申请单
        Application application = new Application();
        application.setId(audit.getApplicationId());
        application.setUpdateTime(new Date());
        AuditQuery auditQuery = new AuditQuery();
        if(auditParam.getAuditStatus().equals(AuditStatusEnum.AUDITED.getCode())){
            //通过
            auditQuery.setApplicationId(auditParam.getApplicationId());
            int count = auditMapper.selectRestAuditCount(auditQuery);
            if(count>0){
                //大于0,还有审批单
                auditQuery.setAuditSort(auditParam.getAuditSort()+1);
                List<AuditVO> auditVOList = auditMapper.selectAudit(auditQuery);
                if(auditVOList != null && auditVOList.size()>0){
                    AuditVO auditVO = auditVOList.get(0);
                    //创建第2个审批单对象用于数据库更新
                    Audit audit2 = new Audit();
                    audit2.setId(auditVO.getId());
                    audit2.setAuditStatus(AuditStatusEnum.MY_PENGDING.getCode());
                    auditMapper.update(audit2);
                }
                application.setStatus(ApplicationStatusEnum.AUDIT.getCode());
                applicationMapper.update(application);
            }else {
                application.setStatus(ApplicationStatusEnum.AUDITED.getCode());
                applicationMapper.update(application);
            }

        }else{
            //驳回
            auditQuery.setApplicationId(audit.getApplicationId());
            List<AuditVO> auditVOList = auditMapper.selectAudit(auditQuery);
            //先判断数据是否为空
            if(auditVOList!=null && auditVOList.size()>0){
                for(int i = 0; i < auditVOList.size(); i++){
                    AuditVO auditVO = auditVOList.get(i);
                    if(auditVO.getAuditStatus().equals(AuditStatusEnum.PENGDING.getCode())){
                        auditMapper.deleteById(auditVO.getId());
                    }
                }
            }
            application.setStatus(ApplicationStatusEnum.REJECT.getCode());
            application.setRejectReason(auditParam.getRejectReason());
            applicationMapper.update(application);

        }
        return row;
    }

    private void assignAuditName(AuditVO auditVO) {
        log.debug("开始执行【根据审批单id查询审批人姓名】业务层方法，参数：{}", auditVO);
        UserQuery userQuery = new UserQuery();
        userQuery.setId(auditVO.getAuditUserId());
        UserVO userVO = userMapper.selectUser(userQuery).get(0);
        auditVO.setAuditUsernameList(userVO.getUsername());
    }
}
